package com.bawei.common.core.domain;


import com.bawei.common.core.constant.Constants;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 响应信息主体
 * @author DongZl
 */
@Data
public class Result<T> extends HashMap<String,Object> {
    private static final long serialVersionUID = 1L;
    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;
    /** 失败 */
    public static final int ERROR = Constants.ERROR;
    private int code;
    private String msg;
    private T data;
    public static <T> Result<T> success() {
        return restResult(null, SUCCESS, null);
    }
    public static <T> Result<T> success(T data) {
        return restResult(data, SUCCESS, null);
    }
    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }
    public static <T> Result<T> error() {
        return restResult(null, ERROR, null);
    }
    public static <T> Result<T> error(String msg) {
        return restResult(null, ERROR, msg);
    }
    public static <T> Result<T> error(T data) {
        return restResult(data, ERROR, null);
    }
    public static <T> Result<T> error(T data, String msg) {
        return restResult(data, ERROR, msg);
    }
    public static <T> Result<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }
    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    /**
     * 成功
     * @return 成功 true
     */
    public boolean isSuccess(){
        return this.code == SUCCESS;
    }

    /**
     * 失败
     * @return 失败 false
     */
    public boolean isError(){
        return !isSuccess();
    }

    @Override
    public Object put(String key, Object value) {
        return super.put(key, value);
    }
}
